package gspushmq.v1.produce.job.sys;

import java.util.Date;
import java.util.Map;

import gspushmq.v1.consumer.core.MessageQueue;
import org.apache.commons.lang3.StringUtils;

import ggframework.mongodb.BasicDBObject;
import ggframework.mongodb.DBCollection;
import ggframework.mongodb.DBCursor;
import ggframework.mongodb.DBObject;
import gspushmq.model.ConsumerWays;
import gspushmq.model.DataMessage;
import gspushmq.model.PrefixEnum;
import gspushmq.model.UserType;
import gspushmq.util.DateUtil;
import gspushmq.util.GGLogger;
import gspushmq.util.MongoUtil;
import gspushmq.util.SysUtil;
import gspushmq.v1.produce.GGDataStorage;
import gspushmq.v1.produce.job.core.GGServiceJob;

/**
 * @Description 系统后台生产消息
 * @Author chengsj
 * @Date 2018/11/14 15:47
 * @Version 1.0
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2015 SunTime Co'Ltd Inc. All rights reserved.
 **/
public class GGSysBackMessageJob extends GGServiceJob {

    private static DBCollection collection;


    @Override
    protected String setJobName() {
        return PrefixEnum.SYSBACK.getMqName();
    }

    @Override
    protected void before() {
        collection = MongoUtil.getFtMixCollection("cs_msg_define");
    }

    @Override
    public void doServiceJob(Map<String, Object> args) {

        // start_time =< now <= end_time,push_status = 1未推送
        DBObject query = new BasicDBObject();
        query.append("push_status", 1);
        query.append("start_time",new BasicDBObject("$lte", new Date()));
        query.append("end_time",new BasicDBObject("$gte",new Date()));
        DBCursor cursor = collection.find(query);
        while (cursor.hasNext()) {
            DBObject o = cursor.next();

            //①后台数据校验
            //消息子类型,非空
            String mqSubType = o.getString("msg_type");
            //消息父类型,非空
            String mqType = o.getString("msg_class");
            //消息跳转模块id
            String moduleType = o.getString("module_code");
            //消息消费对象
            String crowdValue = o.getString("crowd_value");
            //消息消费对象,非空
            String crowdType = o.getString("crowd_type");
            //消息标题
            String title = o.getString("title");
            //消息内容,非空
            String content = o.getString("content");
            //图片地址
            String imgUrl = o.getString("img_url");
            //链接类型
            String linkType = o.getString("link_type");
            //链接
            String linkUrl = o.getString("link_url");
            //target
            String targetId = o.getString("target_id");
            //唯一标识
            Long id = o.getLong("_id");
            //产品线，非空
            Integer productLine = o.getInteger("product_line");
            //有效期,非空
            Date expire = o.getDate("end_time");
            if (StringUtils.isBlank(mqType)
                    || StringUtils.isBlank(mqSubType)
                    || StringUtils.isBlank(content)
                    || StringUtils.isBlank(crowdType)
                    || productLine == null || productLine == 0
                    || expire == null || DateUtil.compare(new Date(), expire) > 0) {
                //后台数据不合法，跳过该不合法数据
                continue;
            }
            //
            DataMessage dm = new DataMessage();
            dm.setMqType(mqType);
            dm.setMqSubType(mqSubType);
            dm.setUserType(UserType.ASSIGN);
            dm.setCrowdType("1".equals(crowdType) ? ConsumerWays.TAG.getWay() : ConsumerWays.ACCOUNT.getWay());
            dm.setCrowdValue(crowdValue);
            dm.setProductLine(productLine);
            dm.setTitle(StringUtils.isBlank(title) ? "系统后台生产！" : title);
            dm.setContent(content);
            dm.setModule(moduleType);
            dm.setTarget(targetId);
            dm.setExpireDate(expire);
            dm.setStartDate(o.getDate("start_time"));
            dm.setLinkUrl(linkUrl);
            dm.setLinkType(linkType);
            dm.setFileAddress(imgUrl);
            try {
                MessageQueue.messageQueue.add(dm);
            } catch (Exception e) {
                GGLogger.error(this.getClass(), "---队列put异常：" + e);
            }
            //更新消息状态，下次不再生产
            collection.update(new BasicDBObject("_id", id), new BasicDBObject("$set", new BasicDBObject("push_status", 2)), false, true);
            //入库
            SysUtil.sendToMongo(dm);
            //日志
            SysUtil.logPattern(GGSysBackMessageJob.class, dm);
        }
        cursor.close();
    }

}
